package com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.HandleTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailAuxiliaryInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailAuxiliaryInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.dto.CaseLegalReviewSaveDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.ICaseLegalReviewService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.CaseLegalReviewInfoVo;
import com.icinfo.framework.tools.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Desc 生态环境调查取证阶段service
 * @Author mahan
 * @Date 2022/11/24
 */
@Service
public class CaseLegalReviewServiceImpl implements ICaseLegalReviewService {

	@Resource
	private CaseMainDetailInfoMapper caseMainDetailInfoMapper;
	@Resource
	private CaseMainDetailAuxiliaryInfoMapper caseMainDetailAuxiliaryInfoMapper;
	@Resource
	private CaseMainInfoMapper caseMainInfoMapper;


	/**
	 * 保存行政处罚审核表基本信息（生态环境）
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public void saveCaseLegalReviewBaseInfo(CaseLegalReviewSaveDto dto) {
		//如果是集体议案，则只保存集体议案状态就行
		CaseMainDetailInfo updateCaseMainDetailInfo = new CaseMainDetailInfo();
		if (YesOrNoEnum.YES.getCode().equals(dto.getIsNeedCollectiveDiscuss())) {
			updateCaseMainDetailInfo.setCaseId(dto.getCaseId());
			updateCaseMainDetailInfo.setIsNeedCollectiveDiscuss(dto.getIsNeedCollectiveDiscuss());
			caseMainDetailInfoMapper.updateByPrimaryKeySelective(updateCaseMainDetailInfo);
		} else {
			// 非分别处罚 更新案件详情表
			if (!StringUtils.equals(HandleTypeEnum.PART_PUNISH.getCode(), dto.getHandleType())) {
				updateCaseMainDetailInfo.setCaseId(dto.getCaseId());
				updateCaseMainDetailInfo.setPunishKinds(dto.getPunishKinds());
				updateCaseMainDetailInfo.setDraftPunishMoney(dto.getDraftPunishMoney());
				updateCaseMainDetailInfo.setConfiscateIllegalMoney(dto.getConfiscateIllegalMoney());
				updateCaseMainDetailInfo.setConfiscateIllegalProperty(dto.getConfiscateIllegalProperty());
				updateCaseMainDetailInfo.setConfiscateIllegalPropertyValue(dto.getConfiscateIllegalPropertyValue());
				updateCaseMainDetailInfo.setPunishContent(dto.getPunishContent());
				updateCaseMainDetailInfo.setInvestConclusion(dto.getInvestConclusion());
				//设置办案结论
				updateCaseMainDetailInfo.setDiscussConclusion(dto.getDiscussConclusion());
			}
			updateCaseMainDetailInfo.setIsNeedCollectiveDiscuss(dto.getIsNeedCollectiveDiscuss());
			caseMainDetailInfoMapper.updateByPrimaryKeySelective(updateCaseMainDetailInfo);

			// 更新案件详情补充表
			if (CollectionUtils.isNotEmpty(dto.getPunishInfoList())) {
				List<CaseMainDetailAuxiliaryInfo> auxiliaryInfoList = JSONObject.parseArray(JSONObject.toJSONString(dto.getPunishInfoList()), CaseMainDetailAuxiliaryInfo.class);
				for (CaseMainDetailAuxiliaryInfo auxiliaryInfo : auxiliaryInfoList) {
					caseMainDetailAuxiliaryInfoMapper.updateByPrimaryKeySelective(auxiliaryInfo);
				}
			}
		}
	}

	/**
	 * 获取行政处罚法制审核表基本信息（生态环境）
	 *
	 * @param caseId
	 * @return
	 */
	@Override
	public CaseLegalReviewInfoVo getCaseLegalReviewBaseInfo(String caseId) {
		CaseLegalReviewInfoVo reVo = new CaseLegalReviewInfoVo();
		//获取主表信息
		CaseMainInfo mainInfo = caseMainInfoMapper.selectByPrimaryKey(caseId);
		reVo.setCaseName(mainInfo.getCaseName());
		reVo.setCaseId(mainInfo.getId());

		CaseMainDetailInfo queryCaseMainDetailInfo = new CaseMainDetailInfo();
		queryCaseMainDetailInfo.setCaseId(caseId);
		CaseMainDetailInfo caseMainDetailInfo = caseMainDetailInfoMapper.selectOne(queryCaseMainDetailInfo);
		reVo.setCaseCbrOpinion(caseMainDetailInfo.getPunishOpinion());
		reVo.setCaseDetail(caseMainDetailInfo.getIllegalFact() + "\n" + caseMainDetailInfo.getCaseInvestConclusion());

		return reVo;
	}

}
